/*
 * BackupMediator.java
 *
 * Created on Oct 12, 2007, 8:36:09 PM
 *
 * Copyright (c) October 14, 2007 by Dean Del Ponte
 * http://www.sourceseed.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by  the Free Software Foundation; either version 3 of the License or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.TXT.  If not, write to
 * the Free Software Foundation Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307 USA
 */
package org.sourceseed.tools.backup.backThatThingUp;

import org.sourceseed.tools.backup.tasks.EmailTrigger;
import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.PropertyConfigurator;

/**
 * Mediator which handles state and actions for the entire application
 *
 * @author Dean Del Ponte
 */
public class BackupMediator implements IMediator {

    static Logger logger = Logger.getLogger(BackupMediator.class);
    private File configFile;
    private Map<String, Project> projectManager;
    private IBackupEngine engine;
    private BackupConfig backupConfig;

    public BackupMediator() throws IOException {
        this(null);
    }

    /**
     * 
     * @param configFile - reference to backupConfig.xml file
     * @throws java.io.IOException 
     */
    public BackupMediator(File configFile) throws IOException {
        PropertyConfigurator.configure("config" + File.separator + "log4j.properties");
        if (configFile == null) {
            this.configFile = getDefaultConfigFile();
        } else {
            this.configFile = configFile;
        }
        if (engine == null) {
            engine = new BackupEngine();
        }
        extractBackupData();

        scheduleBackups();
    }

    /**
     * 
     * @return
     */
    public Collection<Project> getProjects() {
        return projectManager.values();
    }

    /**
     * 
     * @param name
     * @return
     */
    public Project getProject(String name) {
        return projectManager.get(name);
    }

    /**
     * 
     * @param projects
     */
    public void scheduleBackups(Collection<Project> projects) {
        engine.schedule(new ArrayList<Project>(projects), backupConfig);
    }

    /**
     * 
     * @return
     */
    public Map<String, Project> getProjectManager() {
        return projectManager;
    }

    /**
     * 
     * @param projectManager
     */
    public void setProjectManager(Map<String, Project> projectManager) {
        this.projectManager = projectManager;
    }

    /**
     * 
     * @param engine
     */
    public void setBackupEngine(IBackupEngine engine) {
        this.engine = engine;
    }

    /**
     * 
     * @return
     */
    public IBackupEngine getBackupEngine() {
        return engine;
    }

    /**
     * 
     */
    public void cancelBackups() {
        engine.cancel();
    }

    /**
     * 
     * @return
     */
    public String getStatus() {
        return null;
    }

    /**
     * 
     * @return
     */
    protected File getDefaultConfigFile() {
        return new File(new File("config"), "backupConfig.xml");
    }

    private void scheduleBackups() {
        engine.schedule(new ArrayList<Project>(projectManager.values()), backupConfig);
    }

    private void extractBackupData() throws IOException {
        try {
            XStream xstream = new XStream();
            xstream.alias("BackupConfig", BackupConfig.class);
            xstream.alias("Project", Project.class);
            xstream.alias("DayTimes", DayTimes.class);
            xstream.alias("SimpleTime", SimpleTime.class);
            xstream.alias("EmailNotification", EmailNotification.class);
            xstream.alias("EmailTrigger", EmailTrigger.class);
            xstream.alias("MailServer", MailServer.class);
            xstream.alias("DataRetentionLimit", DataRetentionLimit.class);
            xstream.alias("Destination", Destination.class);
            xstream.alias("FtpMode", FtpMode.class);
            setBackupConfig((BackupConfig) xstream.fromXML(FileUtils.readFileToString(configFile)));
            projectManager = new HashMap<String, Project>();
            for (Project p : getBackupConfig().getProjects()) {
                projectManager.put(p.getName(), p);
            }
        } catch (IOException ex) {
            logger.error("Error reading config file", ex);
            throw ex;
        }
    }

    public BackupConfig getBackupConfig() {
        return backupConfig;
    }

    public void setBackupConfig(BackupConfig backupConfig) {
        this.backupConfig = backupConfig;
    }
    
    public void saveBackupConfig(BackupConfig backupConfig) {
        
    }
}
